package com.dartfar.dictionary.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.dictionary.model.Dictcatalog;
import com.dartfar.dictionary.service.DictcatalogService;
import com.dartfar.dictionary.vo.DictcatalogVo;
import com.dartfar.dictionary.vo.DictionaryVo;


@Scope("prototype")
@Component("dictcatalogAction")
public class DictcatalogAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Service对象
	 */
	@Resource
	private DictcatalogService dictcatalogService;

	/**
	 * 页面传参
	 */
	private int id;
	private String dictName;
	private String code;
	private String des;
	
	private String inserted;
	private String updated;
	private String deleted;
	
	public String show(){
		//获取权限
		functions = getAutFunctions();
		return "show";
	}
	
	/**
	 * 保存 综合操作
	 * @param inserted, updated, deleted
	 * @return null
	 */
	public String save(){
		
		List<DictcatalogVo> list;
		//新建键值
		if(inserted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(inserted, DictcatalogVo.class);
			for(Iterator<DictcatalogVo> i =  list.iterator();i.hasNext();){
				DictcatalogVo dict = i.next();
				dictName = dict.getDictName();
				code = dict.getCode();
				des = dict.getDes();
				add();
			}
		}
		//更新键值
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, DictcatalogVo.class);
			for(Iterator<DictcatalogVo> i =  list.iterator();i.hasNext();){
				DictcatalogVo dict = i.next();
				id = dict.getId();
				dictName = dict.getDictName();
				code = dict.getCode();
				des = dict.getDes();
				edit();
			}
		}
		//删除键值
		if(deleted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(deleted, DictcatalogVo.class);
			for(Iterator<DictcatalogVo> i =  list.iterator();i.hasNext();){
				DictcatalogVo dict = i.next();
				id = dict.getId();
				remove();
			}
		}
		return null;
	}
	
	public String add(){
		Dictcatalog dict = new Dictcatalog();
		dict.setDictName(dictName);
		dict.setCode(code);
		dict.setDes(des);
		dictcatalogService.save(dict);
		return null;
	}
	
	public String edit(){
		Dictcatalog dict = dictcatalogService.get(id);
		dict.setDictName(dictName);
		dict.setCode(code);
		dict.setDes(des);
		dictcatalogService.update(dict);
		return null;
	}

	public String remove(){
		dictcatalogService.delete(dictcatalogService.get(id));
		return null;
	}
	
	public String loadDictcatalogData(){
		List<DictcatalogVo> dictcatalogVos = new ArrayList<DictcatalogVo>();
		DataGridVo<DictcatalogVo> dictcatalogDataGridVo = new DataGridVo<DictcatalogVo>();
		try{
		if (curUser() != null){
			List<Dictcatalog> list = dictcatalogService.getListForPage(pageNo, pageSize);	//查看全部
			
			System.out.println("list: " + list.size());
			Long dictionaryCount = dictcatalogService.getCount();
			for(Iterator<Dictcatalog> i =  list.iterator();i.hasNext();){
				Dictcatalog n = i.next();
				DictcatalogVo dictcatalogVo = new DictcatalogVo();
				//写入赋值代码
				dictcatalogVo.setId(n.getId());
				dictcatalogVo.setDictName(n.getDictName());
				dictcatalogVo.setCode(n.getCode());
				dictcatalogVo.setDes(n.getDes());
				dictcatalogVos.add(dictcatalogVo);
			}
			dictcatalogDataGridVo.setTotal(dictionaryCount);
			dictcatalogDataGridVo.setRows(dictcatalogVos);

		}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		String jsonResult = JSONObject.fromObject(dictcatalogDataGridVo).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 *  Gets And Sets
	 */
	public String getDictName() {
		return dictName;
	}

	public void setDictName(String dictName) {
		this.dictName = dictName;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getInserted() {
		return inserted;
	}

	public void setInserted(String inserted) {
		this.inserted = inserted;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getDeleted() {
		return deleted;
	}

	public void setDeleted(String deleted) {
		this.deleted = deleted;
	}
	
	
	
	
}
